(function($, doc) {
	$.plusReady(function() {
		mui.init();
		var user = app.getState();
		var uid = user.user_id;
		var postpass = md5(user.password);
		plus.nativeUI.showWaiting("钱包同步中...");
		ethscan.getSysAmount(function(money, addr){
			doc.getElementById('addr').innerHTML = addr;
			doc.getElementById('money').innerHTML = money;
			plus.nativeUI.closeWaiting();
		});
		/*
		var defAddr = defCoin['address'];
		var myWallet = ethscan.getWallet('eth');
		var provider = ethers.providers.getDefaultProvider('homestead');
		//console.log(user.eth_address);
		var wallet = new ethers.Wallet(myWallet.privkey, provider);
		var contracts = new ethers.Contract(myWallet.privkey, defCoin['abi'], wallet);
		//var contracts = new ethers.Contract(defAddr, defCoin['abi'], provider);
		doc.getElementById('addr').innerHTML = myWallet.address;
		plus.nativeUI.showWaiting("钱包同步中...");
		contracts.balanceOf(myWallet.address).then(function(res) {
			var etherString = ethers.utils.formatEther(res);
			//console.log(JSON.stringify(etherString));
			doc.getElementById('money').innerHTML = etherString;
			plus.nativeUI.closeWaiting();
		});*/
		var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p=" + postpass;
		//console.log(url);
		mui.getJSON(url, function(res) {
			if(res.status == 1) {
				document.getElementById("coin_balance").innerHTML = res.data.a1_money;
			}
		});
		var getCodeBox = document.getElementById('getCode');
		var c1 = 60;
		getCodeBox && getCodeBox.addEventListener('tap', function() {
			console.log(appurl + 'ChatApi/getsms');
			mui.post(appurl + 'ChatApi/getsms', {
				u: app.getState().user_id,
				smstype: 8
			}, function(res) {
				console.log(JSON.stringify(res));
				if(res.status == 1) {
					mui.toast('发送短信成功');
				} else {
					mui.toast(res.msg);
					getCodeBox.innerHTML = '获取验证码';
					getCodeBox.removeAttribute('disabled');
					c1 = 60;
					return;
				}
				var code1 = setInterval(function() {
					getCodeBox.innerHTML = c1 + '秒后重新获取';
					c1 = c1 - 1;
					if(c1 >= 0) {
						getCodeBox.setAttribute("disabled", "disabled");
					} else {
						getCodeBox.innerHTML = '获取验证码';
						getCodeBox.removeAttribute('disabled');
						c1 = 60;
						clearInterval(code1);
					}
				}, 1000);
			});
		})
		doc.getElementById('sub').addEventListener('tap', function() {
			var count = doc.getElementById("count").value;
			var password_account = doc.getElementById("password_account").value;
			var code = document.getElementById('code').value;
			if(count == "") {
				$.alert("请输入充币数量");
				return;
			}
			if(password_account == "") {
				$.alert("请输入交易密码");
				return;
			}
			if(!code || code == '') {
					mui.alert('请输入手机验证码！');
					return;
				}
			var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p1=" + password_account;
			//plus.nativeUI.showWaiting("钱包校验中...");
			console.log(url);
			mui.getJSON(url, function(res) {
				console.log(JSON.stringify(res));
				var count = doc.getElementById("count").value * 1;
				
				//return;
				var a1money = res.data.a1_money * 1;

				//console.log(a1money);
				var rate = res.data.rate * 1;
				if(res && res.status == 1 && a1money > count) {
					
					var url = weburl + "/home/ChatApi/tilian.html";
					$.post(url, {
						u: uid,
						p: postpass,
						addr : doc.getElementById('addr').innerHTML,
						c: count,
						code : code,
						smstype : 8,
						p1: password_account,
						hash: '',
						note: '充链备注'
					}, function(res) {
						//console.log(JSON.stringify(res));

						$.alert(res.msg);
						plus.webview.currentWebview().close();
						return;
					});
					
					/*
					var wallet = new ethers.Wallet(sysethprivkey, provider);
					var contracts = new ethers.Contract(defAddr, defCoin['abi'], wallet);
					count1 = ((100 - rate)/100)*count;
					//console.log(count);
					var amount = ethers.utils.parseEther(count1.toString());
					//console.log(amount);
					//return;
					var options = {
						//gasPrice: 1100000000, // 1.1Gwei
						gasLimit: 300000
					};
					var callPromise = contracts.transfer(user.eth_address, amount, options);
					callPromise.then(function(obj) {
						console.log(JSON.stringify(obj));
						//$.toast('付款成功！');
						//dopost(obj.hash, toaddr);
						plus.nativeUI.closeWaiting();
						var url = weburl + "/home/ChatApi/tilian.html";
						$.post(url, {
							u: uid,
							p: postpass,
							c: count,
							p1: password_account,
							hash: obj.hash,
							note: '充链备注'
						}, function(res) {
							console.log(JSON.stringify(res));
							
							$.alert(res.msg);
							return;
						});

					})

					callPromise.catch(function(error) {
						console.log(JSON.stringify(error));
						$.toast('充链失败！');
						plus.nativeUI.closeWaiting();
						return;
					});*/
				} else {
					$.alert('密码错误或余额不足！');
					//plus.nativeUI.closeWaiting();
					return;
				}
			});

		});
	});
}(mui, document));